﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace LogisticsDAL
{
    public static class SqlHelper
    {
        public static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        
        /// <summary>
        /// 查询sql，返回第一行第一列
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="parameters">参数化</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql,params SqlParameter [] parameters)
        {
            using (SqlConnection con =new SqlConnection(conStr))
            {
                if (con.State==System.Data.ConnectionState.Closed)
                {
                    con.Open();
                }
                using (SqlCommand cmd=new SqlCommand(sql,con))
                {
                    if (parameters!=null)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }
                    return cmd.ExecuteScalar();
                }
            }
        
        
        }
        /// <summary>
        /// 执行sql语句返回一个DataReader
        /// 当返回DataReader的时候，注意：
        /// 1.Connection不能关闭
        /// 2.DataReader不能关闭
        /// 3.command对象执行ExecuteReader()的时候需要传递一个参数CommandBehavior.CloseConnection
        /// </summary>
        /// <param name="sql">查询语句</param>
        /// <param name="parameters">将需要的条件参数化</param>
        /// <returns>SqlDataReader对象</returns>
        public static SqlDataReader ExecuteReader(string sql,params SqlParameter [] parameters)
        {
            SqlConnection con = new SqlConnection(conStr);
            if (con.State == System.Data.ConnectionState.Closed)
            {
                con.Open();
            }
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (parameters != null && parameters.Length > 0)
                {
                    cmd.Parameters.AddRange(parameters);
                }
                return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
            }
        }

        /// <summary>
        /// 增删改操作
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="parameters">将需要的条件参数化</param>
        /// <returns>int类型</returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                if (con.State == System.Data.ConnectionState.Closed)
                {
                    con.Open();
                }


                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (parameters != null && parameters.Length > 0)
                    {
                        cmd.Parameters.AddRange(parameters);
                    }

                    return cmd.ExecuteNonQuery();

                }
            }
        }

    }
}